//
//  MILAuthorizationManagerViewController.m
//  MicrowaveInteractionLight
//
//  Created by maple on 2016/12/17.
//  Copyright © 2016年 maple. All rights reserved.
//

#import "MILAuthorizationManagerViewController.h"
#import "ScanfQRCodeView.h"
#import "CustomAlertView.h"
#import "ShareDeviceApi.h"

@interface MILAuthorizationManagerViewController ()<ScanfQRCodeViewDelegate>
{
    
    ScanfQRCodeView *scanfView_;
}
@property (weak, nonatomic) IBOutlet UIImageView *scanImageView;

@property (strong, nonatomic) UIView *bgView;
@property (strong, nonatomic) UIImageView *line;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property(nonatomic,strong)ThemeAlertView *themeAlertView;
@end
@implementation MILAuthorizationManagerViewController

#pragma mark -- UI
-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor blackColor];
    scanfView_ = [[ScanfQRCodeView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    scanfView_.delegate = self;
    
    
    [self.view addSubview:scanfView_];
    [self.view sendSubviewToBack:scanfView_];
    
}

- (UIImageView *)line
{
    if (!_line) {
        _line = [[UIImageView alloc]initWithFrame:CGRectMake(30, 0, self.scanImageView.frame.size.width - 60, 3)];
        [_line setImage:[UIImage imageNamed:@"saomiaotiao"]];
        [self.scanImageView addSubview:_line];
    }
    return _line;
}

- (UIView *)bgView
{
    if (!_bgView) {
        // 这里创建指引在这个视图在window上
        CGRect frame = [UIScreen mainScreen].bounds;
        UIView * bgView = [[UIView alloc]initWithFrame:frame];
        _bgView = bgView;
        bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.65];
        [self.view addSubview:bgView];
        
        //create path 重点来了（**这里需要添加第一个路径）
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:frame];
        // 这里添加第二个路径 （这个是矩形）
        [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(self.scanImageView.frame.origin.x, self.scanImageView.frame.origin.y, self.scanImageView.frame.size.width, self.scanImageView.frame.size.height) cornerRadius:0] bezierPathByReversingPath]];
        
        CAShapeLayer *shapeLayer = [CAShapeLayer layer];
        shapeLayer.path = path.CGPath;
        [bgView.layer setMask:shapeLayer];
 
        [self.view bringSubviewToFront:self.label];
    }
    return _bgView;
}

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [scanfView_ CancelRunning];
    [self.scanImageView.layer removeAllAnimations];
}

- (void)viewWillDisappear:(BOOL)animated{
    //停止动画
    [super viewWillDisappear:animated];
    
}


-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [scanfView_ startScan];
    
    [self bgView];
    [self line];
    //启动动画
    
    [UIView animateWithDuration:2 delay:0 options:UIViewAnimationOptionRepeat animations:^{
        _line.center = CGPointMake(_line.center.x, self.scanImageView.frame.size.height);
    } completion:nil];
    
    [self.view bringSubviewToFront:self.navigationBar];

}

- (void)refreshBaseControlValue
{
    [self setNavigationBarTitle:@"二维码权限"];

    [self hideNavigationBottomLine];

}

#pragma mark -- 扫描结果
- (void) scanfQRCode:(ScanfQRCodeView *)scanfview result:(NSString *)result{
    
    [self.scanImageView.layer removeAllAnimations];
    //扫描出结果 弹框
    NSLog(@"result = %@",result);
    /* 停止
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        _line.center = CGPointMake(_line.center.x, 0);
    } completion:nil];
     */
//    UILabel *mytitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH*0.8, 50)];
//    mytitleLabel.text = @"确认授予权限吗？";
//    mytitleLabel.textAlignment = NSTextAlignmentCenter;
    
    
    
//    if (![result isMobileNumber]) {
//        [SVProgressHUD showErrorWithStatus:@"请扫描正确的二维码"];
//        [scanfView_ startScan];

//        return;
//    }
    
    ThemeAlertView *alertView = [[ThemeAlertView alloc]initWithType:0];
    alertView.textField.userInteractionEnabled = NO;
    alertView.textField.text = @"确认授予权限吗？";
    [self.view addSubview:alertView];
    
    [alertView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    
    [alertView callinAnimation];
    alertView.enter_block = ^(UIButton *sender) {
        ShareDeviceApi *api = [[ShareDeviceApi alloc]initWithPar:@{@"id":self.myDeviceModel.ID,@"tuid":result}];
        [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            NSDictionary * myDic = [request responseJSONObject];
            NSLog(@"my Dic == %@ ",myDic);
            if ([myDic[@"errcode"] intValue] == 0) {
                [SVProgressHUD showSuccessWithStatus:@"分享设备成功"];
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else{
                [SVProgressHUD showErrorWithStatus:myDic[@"errmsg"]];
                
            }
            
            
        } failure:^(YTKBaseRequest *request) {
            
            [SVProgressHUD showErrorWithStatus:@"请求失败"];
        }];

    };
    alertView.cancel_block = ^(UIButton *sender) {
        [self.navigationController popViewControllerAnimated:YES];
    };
    

}

#pragma mark -- CustomAlertViewDelegate
- (void)alertView:(CustomAlertView *)alertView didClickButtonAtIndex:(NSInteger)index
{
    switch (index)
    {
        case 0:
        {
            NSLog(@"sure");
            ShareDeviceApi *api = [[ShareDeviceApi alloc]initWithPar:@{@"id":self.myDeviceModel.ID,@"mobile":EXRACT_OBJECT(USERNAME)}];
            [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
                NSDictionary * myDic = [request responseJSONObject];
                NSLog(@"my Dic == %@ ",myDic);
                if ([myDic[@"errcode"] intValue] == 0) {
                    [SVProgressHUD showSuccessWithStatus:@"分享设备成功"];
                    [self.navigationController popToRootViewControllerAnimated:YES];
                }else{
                    [SVProgressHUD showErrorWithStatus:myDic[@"errmsg"]];
                }
                
                
            } failure:^(YTKBaseRequest *request) {
                
                [SVProgressHUD showErrorWithStatus:@"请求失败"];
            }];

        }
            break;
        case 1:
        {
            NSLog(@"cancel");
        }
            break;
            
        default:
            break;
    }
    [CustomAlertView dissmiss];
    [self.navigationController popViewControllerAnimated:YES];
}

- (ThemeAlertView *)themeAlertView
{
    if(!_themeAlertView)
    {
        UIView *superView = self.view;
        _themeAlertView = [[ThemeAlertView alloc]init];
        [superView addSubview:_themeAlertView];
    }
    return _themeAlertView;
}

@end
